package org.benjamin.lending.configuration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.service.EmailService;
import org.benjamin.lending.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("org.benjamin.lending.controller")
public class ApplicationServletConfiguration extends WebMvcConfigurerAdapter{
	
	private final Logger logger = LogManager.getLogger();

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
		super.configureDefaultServletHandling(configurer);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/js/**","/css/**","/json/**","/fonts/**","/plugins/**","/themes/*")
		.addResourceLocations("/js/","/css/","/json/","/fonts/","/plugins/","/themes/");
		super.addResourceHandlers(registry);
	}
	
	@Bean
	@Order(1)
	public TilesConfigurer tilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();
		tilesConfigurer.setDefinitions("/WEB-INF/definitions/tiles.xml","/WEB-INF/definitions/tiles-config.xml");
		tilesConfigurer.setCheckRefresh(true);
		return tilesConfigurer;
	}
	
	@Bean
	public TilesViewResolver tilesViewResolver() {
		return new TilesViewResolver(); 
	}
	
	@Bean
	public InternalResourceViewResolver internalResourceViewResolver() {
		InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
		internalResourceViewResolver.setPrefix("/WEB-INF/views/");
		internalResourceViewResolver.setSuffix(".jsp");
		return internalResourceViewResolver;
	}
	
	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(Long.parseLong("10737418240"));
		return multipartResolver;
	}
	
	
	
	@Bean(name = "/EmailService")
	public HttpInvokerServiceExporter emailService(EmailService emailService) {
		HttpInvokerServiceExporter serviceExporter = new HttpInvokerServiceExporter();
		serviceExporter.setService(emailService);
		serviceExporter.setServiceInterface(EmailService.class);
		return serviceExporter;
	}
	
	@Bean(name = "/MemberServicer")
	public HttpInvokerServiceExporter memberServicer(MemberService memberService) {
		HttpInvokerServiceExporter serviceExporter = new HttpInvokerServiceExporter();
		serviceExporter.setService(memberService);
		serviceExporter.setServiceInterface(MemberService.class);
		return serviceExporter;
	}
	
	
	
}
